package com.elong.businesstravel.modules.home.dialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.DateUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.modules.home.adapter.SimpleGridDialogAdapter;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 信用卡有效期
 * 
 * @author lenovo
 * 
 */
public class CreditCardValidityDialog extends Dialog {

	private Context mContext;
	private GridView mGridView;
	private SimpleGridDialogAdapter mSimpleGridAdapter;
	private OnItemClickListener mOnItemClickListener;

	private ImageView mLeftImageView;
	private ImageView mRightImageView;
	private TextView mYearTextView;

	private String mYearString;
	private String mMonthString;
	private List<Integer> mYearList = new ArrayList<Integer>();
	private int mYearIndex;
	private int tempCurrentYear;

	public CreditCardValidityDialog(Context context) {
		super(context);
		init(context);
	}

	public CreditCardValidityDialog(Context context, int theme,
			String yearString, String monthString) {
		super(context, theme);
		mYearString = yearString;
		mMonthString = monthString;
		init(context);
	}

	public CreditCardValidityDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	private void init(Context context) {
		mContext = context;
		getWindow().setGravity(Gravity.BOTTOM);
		setContentView(R.layout.dialog_credit_card_validity);

		int currentYear = Integer.parseInt(DateUtils.getCurrentDate("yyyy"));
		tempCurrentYear = currentYear;

		for (int i = 0; i < 10; i++) {
			mYearList.add(currentYear);
			currentYear++;
		}

		for (int i = 0; i < mYearList.size(); i++) {
			if (mYearString.equals(mYearList.get(i) + "年")) {
				mYearIndex = i;
				break;
			}
		}

		mYearTextView = (TextView) findViewById(R.id.yearTextView);
		mYearTextView.setText(mYearList.get(mYearIndex) + "年");
		mLeftImageView = (ImageView) findViewById(R.id.leftImageView);
		mRightImageView = (ImageView) findViewById(R.id.rightImageView);

		mGridView = (GridView) findViewById(R.id.gridView);
		
		setSimpleGridDialog();

		setListener();

		setCanceledOnTouchOutside(true);
	}
	
	private void setSimpleGridDialog() {
		List<String> disable = new ArrayList<String>();
		List<String> monthList = new ArrayList<String>();
		int currentMonth = 0;
		try {
			currentMonth = Integer.parseInt(DateUtils
					.getCurrentDate("MM"));
		} catch (Exception e) {
		}
		for (int i = 0; i < 12; i++) {
			if (currentMonth < i + 1) {

			} else {
				if (tempCurrentYear == mYearList.get(mYearIndex)) {
					disable.add((i + 1) + "月");
				}
			}
			monthList.add((i + 1) + "月");
		}

		mSimpleGridAdapter = new SimpleGridDialogAdapter(mContext,
				monthList, mMonthString, disable);
		mGridView.setAdapter(mSimpleGridAdapter);
	}

	protected void setListener() {
		mLeftImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mYearIndex--;
				if (mYearIndex <= -1) {
					mYearIndex = -1;
				} else {

					mYearTextView.setText(mYearList.get(mYearIndex) + "年");

					setSimpleGridDialog();
				}
			}
		});

		mRightImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mYearIndex++;
				if (mYearIndex >= mYearList.size()) {
					mYearIndex = mYearList.size();
				} else {
					mYearTextView.setText(mYearList.get(mYearIndex) + "年");

					setSimpleGridDialog();
				}
			}
		});

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if (null != mOnItemClickListener) {
					String itemString = (String) mSimpleGridAdapter
							.getItem(position);
					if (mYearIndex <= -1) {
						mYearIndex = 0;
					}
					if (mYearIndex >= mYearList.size()) {
						mYearIndex = mYearList.size() - 1;
					}

					String dateString = mYearList.get(mYearIndex) + "年"
							+ itemString;// yyyy年MM月
					String currentDateString = DateUtils
							.getCurrentDate("yyyy年MM月");

					Calendar dateCalendar = Calendar.getInstance();
					dateCalendar.setTimeInMillis(DateUtils.getDateMillis(
							dateString, "yyyy年MM月"));

					Calendar currentCalendar = Calendar.getInstance();
					currentCalendar.setTimeInMillis(DateUtils.getDateMillis(
							currentDateString, "yyyy年MM月"));

					if (currentCalendar.compareTo(dateCalendar) >= 0) {
//						HandlerToastUI.getHandlerToastUI(mContext, "请选择正确的时间");
					} else {

						view.setTag(mYearList.get(mYearIndex) + "年"
								+ itemString);
						mOnItemClickListener.onItemClick(parent, view,
								position, id);
						cancel();
					}
				}
			}
		});
	}

	public void setOnItemListener(OnItemClickListener onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}

}
